import sys
import warnings
warnings.filterwarnings('ignore')
sys.path.append('../..')
from common.config import *
from db.mysql import MysqlClient
import requests
from common.helper import Helper
from pyquery import PyQuery as pq

class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "药无忧-抗癌特药保障计划"
    def genProductInfo(self):
        r = requests.get( url = domain[self.name]["url"],headers=domain[self.name]["header"] )
        if r.status_code == 200:
            doc = pq(r.text)
            for sp in range(2,6):
                tbody = doc("tbody tr").eq(sp)
                tbs = tbody.text().split("\n")
                data = {
                    # 年龄段
                    "year":tbs[0],
                    # 有无社保
                    "socialSec":"Y",
                    #保额
                    "money":"2000000",
                    #保障期限
                    "expire":"1年",
                    # 交费方式
                    "payType":"年交",
                    #产品价格
                    "price":Helper.decimalParse( int(tbs[1] ) )
                }
                self.mysql.cursor.execute(domain[self.name]["insert_sql"].format(**data))
                self.mysql._conn.commit()
                print(domain[self.name]["insert_sql"].format(**data))
                data = {
                    # 年龄段
                    "year":tbs[0],
                    # 有无社保
                    "socialSec":"N",
                    #保额
                    "money":"2000000",
                    #保障期限
                    "expire":"1年",
                    # 交费方式
                    "payType":"年交",
                    #产品价格
                    "price":Helper.decimalParse( int(tbs[2] ) )
                }
                self.mysql.cursor.execute( domain[self.name]["insert_sql"].format( **data ))
                self.mysql._conn.commit()
                print( domain[self.name]["insert_sql"].format( **data ) )

